package com.icez.in_app_notication_library

import android.animation.ObjectAnimator
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.HapticFeedbackConstants
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.view.animation.AnimationUtils
import android.view.animation.AnticipateOvershootInterpolator
import android.view.animation.OvershootInterpolator
import android.widget.Button
import android.widget.FrameLayout
import android.widget.ProgressBar
import androidx.appcompat.widget.AppCompatImageView
import java.util.*

class InAppNoticationLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {
    companion object {
        const val ANIMATION_DURATION: Long = 500
    }


    private var enabledVibration = false



    // 只允许动画一次
    private var onlyOnce = true

    private fun initConfiguration() {
        if (enabledVibration) {
            performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)
        }
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        initConfiguration()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (onlyOnce) {
            onlyOnce = false
            AnimatorUtil.instance.objectAnimator(
                this,
                AnimatorUtil.ObjectAnimatorType.TRANSLATIONY,
                null,
                null,
                {
                    it.interpolator = OvershootInterpolator()
                    it.duration = ANIMATION_DURATION
                },
                null, -measuredHeight.toFloat(),0f
            )?.start()
        }
    }

   

    init {

    }

    /**
     * 隐藏组件
     * @param removeNow 是否立即移除
     * @sample
     * @author Icez
     */
    fun hide(removeNow: Boolean = false) {
        //判断是否附加到窗口
        if (!isAttachedToWindow) {
            //没有附加到窗口
            return
        }
        //获取窗口服务
        val windowManager = (this.context.getSystemService(Context.WINDOW_SERVICE) as? WindowManager) ?: return
        if (removeNow) {//立即从窗口中移除
            if (isAttachedToWindow) {//当前控件是否附加到窗口
                //窗口移除当前控件
                windowManager.removeViewImmediate(this)
            }
        }else{
            AnimatorUtil.instance.objectAnimator(this, AnimatorUtil.ObjectAnimatorType.TRANSLATIONY, null, null, {
                it.interpolator = AnticipateOvershootInterpolator()
                it.duration = ANIMATION_DURATION
            }, null, 0f, -measuredHeight.toFloat())?.start()

            Handler(Looper.getMainLooper()).postDelayed({
                if (isAttachedToWindow) {
                    windowManager.removeViewImmediate(this)
                }
            }, ANIMATION_DURATION)
        }


    }

    
    /**
     * 触摸移动关闭方向
     * @sample 
     * @author Icez
     */
    fun setOnTouchDismiss(mInAppNoticationDismissTouchType:InAppNoticationDismissTouchType){
        setOnTouchListener(InAppNoticationDismissTouchListener(this,mInAppNoticationDismissTouchType))
    }

}

